#include "BlockQueue.hpp"
#include <time.h>
#include <cstdlib>
#include "task.hpp"

using namespace NSBlockQueue;
using namespace NSTask;

static char sign[] = "+-*/%";

void *ConsumerRun(void *args)
{
    BlockQueue<task> *bq = (BlockQueue<task> *)args;
    task t;
    
    while (true)
    {
        bq->Pop(&t);
        int data = t();
        std::cout << "消费者处理了一个任务: " <<  data << std::endl;
    }
}

void *ProducerRun(void *args)
{
    BlockQueue<task> *bq = (BlockQueue<task> *)args;
    while (true)
    {
        int x = rand() % 20 + 1;
        int y = rand() % 20 + 1;
        char ch = sign[rand() % 5];
        std::cout << "生产者生成了一个任务：" << x << ch << y << "=?" << std::endl;
        bq->Push(task(x,y,ch));
        sleep(1);
    }
}

int main()
{
    srand((unsigned)time(nullptr));
    BlockQueue<task> *bq = new BlockQueue<task>();

    pthread_t consumer, prodecer;
    pthread_create(&consumer, nullptr, ConsumerRun, (void *)bq);
    pthread_create(&prodecer, nullptr, ProducerRun, (void *)bq);

    pthread_join(consumer, nullptr);
    pthread_join(prodecer, nullptr);
    return 0;
}